import { NotifyPlugin, MessagePlugin, DialogPlugin } from 'tdesign-vue-next';

export const setupTdesignTools = (app) => {
	const dialog = (body, title = '系统提示', confirm, close, cancelBtn = null, theme = null, placement = 'top') => {
		const dialog = DialogPlugin({
			className: 'wbh-dialog',
			header: title,
			body: body,
			confirmBtn: '确定',
			cancelBtn: cancelBtn,
			closeBtn: false,
			closeOnEscKeydown: false,
			closeOnOverlayClick: false,
			placement,
			theme,
			top: '30vh',
			onConfirm: () => {
				confirm && confirm();
				dialog.hide();
			},
			onClose: ({ e, trigger }) => {
				close && close();
				dialog.hide();
			},
		});
		return dialog;
	};

	app.config.globalProperties.$baseAlert = window.$baseAlert = {
		info: (body, title = '系统提示', confirm, close, placement) => {
			return dialog(body, title, confirm, close, null, 'info', placement);
		},
		warning: (body, title = '系统提示', confirm, close, placement) => {
			return dialog(body, title, confirm, close, null, 'warning', placement);
		},
		danger: (body, title = '系统提示', confirm, close, placement) => {
			return dialog(body, title, confirm, close, null, 'danger', placement);
		},
		success: (body, title = '系统提示', confirm, close, placement) => {
			return dialog(body, title, confirm, close, null, 'success', placement);
		},
		default: (body, title = '系统提示', confirm, close, placement) => {
			return dialog(body, title, confirm, close, null, 'default', placement);
		},
		msg: (body, title = '系统提示', confirm, close, placement) => {
			return dialog(body, title, confirm, close, null, 'default', placement);
		},
	};

	app.config.globalProperties.$baseConfirm = window.$baseConfirm = (
		body,
		title = '系统提示',
		confirm,
		close,
		cancelBtn = '取消',
		theme = 'warning',
		placement
	) => {
		const d = dialog(
			body,
			title,
			confirm,
			close,
			() => {
				return h(
					TButton,
					{
						theme: 'warning',
						variant: 'outline',
						onclick: () => {
							close && close();
							d.hide();
						},
					},
					() => cancelBtn
				);
			},
			theme,
			placement
		);
		return d;
	};

	const notify = (content, title = '系统提示', theme = 'info', close, durationEnd, duration = 3000) => {
		NotifyPlugin(theme, {
			content,
			title,
			closeBtn: true,
			duration,
			onCloseBtnClick: () => {
				close && close();
			},
			onDurationEnd: () => {
				durationEnd && durationEnd();
			},
		});
	};

	app.config.globalProperties.$baseNotify = window.$baseNotify = {
		info: (content, title = '系统提示', close, durationEnd, duration = 3000) => {
			notify(content, title, 'info', close, durationEnd, duration);
		},
		warning: (content, title = '系统提示', close, durationEnd, duration = 3000) => {
			notify(content, title, 'warning', close, durationEnd, duration);
		},
		danger: (content, title = '系统提示', close, durationEnd, duration = 3000) => {
			notify(content, title, 'danger', close, durationEnd, duration);
		},
		success: (content, title = '系统提示', close, durationEnd, duration = 3000) => {
			notify(content, title, 'success', close, durationEnd, duration);
		},
		default: (content, title = '系统提示', close, durationEnd, duration = 3000) => {
			notify(content, title, 'default', close, durationEnd, duration);
		},
		msg: (content, title = '系统提示', close, durationEnd, duration = 3000) => {
			notify(content, title, 'default', close, durationEnd, duration);
		},
	};

	const msg = (content, theme, close, closeBtn, duration) => {
		MessagePlugin(theme, content, {
			closeBtn,
			duration,
			onClose: () => {
				close && close();
			},
		});
	};
	app.config.globalProperties.$baseMsg = window.$baseMsg = {
		info: (content, close, closeBtn, duration) => {
			msg(content, 'info', close, closeBtn, duration);
		},
		warning: (content, close, closeBtn, duration) => {
			msg(content, 'warning', close, closeBtn, duration);
		},
		danger: (content, close, closeBtn, duration) => {
			msg(content, 'danger', close, closeBtn, duration);
		},
		success: (content, close, closeBtn, duration) => {
			msg(content, 'success', close, closeBtn, duration);
		},
		default: (content, close, closeBtn, duration) => {
			msg(content, 'default', close, closeBtn, duration);
		},
		msg: (content, close, closeBtn, duration) => {
			msg(content, 'default', close, closeBtn, duration);
		},
	};
};
